package com.taxigang.client.ui.map;

import com.allen_sauer.gwt.log.client.Log;
import com.google.code.gwt.geolocation.client.Coordinates;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.taxigang.client.TaxiGangHandler;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.client.uibinder.SearchAndCreate;
import com.taxigang.client.uibinder.TaxiGangMapPanel;
import com.taxigang.client.uibinder_mobile.TaxiGangMobile;
import com.taxigang.shared.TaxiGangInfo;

public class MobileNavigatorManager {
//	public static String staticStartAddress;
//	public static String staticDestinationAddress;
	
	private static NavigatorWidget navigatorView;

	private static void executeNavigator(final Command cmd, final TaxiGangMobile taxiGangMobile){
		if (navigatorView == null){
			Log.info("executeNavigator creating new");
			GWT.runAsync(new RunAsyncCallback() {
				public void onFailure(Throwable caught) {
					Window.alert("Code download failed");
				}

				public void onSuccess() {
					navigatorView = NavigatorWidget.getInstance(taxiGangMobile);
					navigatorView.showMapMobile(taxiGangMobile,cmd);
				}
			});

		} else {
			Log.info("executeNavigator use existing");
			cmd.execute();
		}
	}

	public static void init(final TaxiGangMobile taxiGangMobile) {
		executeNavigator(null,taxiGangMobile);
	}

	public static void createStartMarker(final String start, final TaxiGangMobile taxiGangMobile) {
		Log.info("MobileNavigatorManager:createStartMarker");
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createStartMarker(start,false);
				
			}
			
		};
		executeNavigator(cmd,taxiGangMobile);
	}

//	public static void createStartMarker(final String address, final TaxiGangMobile taxiGangMobile) {
//		Log.info("MobileNavigatorManager:createStartMarker");
//		Command cmd = new Command(){
//
//			@Override
//			public void execute() {
//				navigatorView.createStartMarker(address,(Command)null,false);
//				
//			}
//			
//		};
//		executeNavigator(cmd,taxiGangMobile);
//	}
	
	public static void createStartMarker(final Coordinates c, final TaxiGangMobile taxiGangMobile, final boolean setAddress, final Command cmd2) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createStartMarker(c,cmd2,setAddress);
				
			}
		};
		executeNavigator(cmd,taxiGangMobile);
		
	}


	public static void createDestinationMarker(final String destination, final TaxiGangMobile taxiGangMobile) {
		Log.info("MobileNavigatorManager:createDestinationMarker");
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.createDestinationMarker(destination,true,null);
				
			}
			
		};
		executeNavigator(cmd,taxiGangMobile);
	}

//	public static void createDestinationMarker(final String address, final TaxiGangMobile taxiGangMobile) {
//		Log.info("MobileNavigatorManager:createDestinationMarker");
//		Command cmd = new Command(){
//
//			@Override
//			public void execute() {
//				navigatorView.createDestinationMarker(address,(Command)null,true);
//				
//			}
//			
//		};
//		executeNavigator(cmd,taxiGangMobile);
//	}

	
	public static void createStartDestinationMarker(final String startAddress,
			final String destinationAddress, final TaxiGangMobile taxiGangMobile) {
		Log.info("MobileNavigatorManager:createStartDestinationMarker");

		Command cmd = new Command(){

			@Override
			public void execute() {
				Log.info("cmd:createStartDestinationMarker");

				navigatorView.createStartMarker(startAddress,(Command)null,false);
				navigatorView.createDestinationMarker(destinationAddress,(Command)null,true);
				
			}
			
		};
		executeNavigator(cmd,taxiGangMobile);
		
	}

//	public static void show(final TaxiGangInfo taxiGangInfo, final TaxiGangMobile taxiGangMobile) {
//				
//		final Command startDestCmd = new Command(){
//
//			@Override
//			public void execute() {
//				Log.info("cmd:setUpForCreation");
//				Command destCommand = new Command(){
//
//					@Override
//					public void execute() {
//						
//						navigatorView.createStartMarker(taxiGangInfo.getStartAddress(),(Command)null,false);
//
//
//					}};
//
//				navigatorView.createStartMarker(taxiGangInfo.getStartAddress(),destCommand,true);
//
//			}
//			
//		};
//
//		executeNavigator(startDestCmd,taxiGangMobile);
//		
//	}

	public static void searchForTaxiGangs(final String startAddress,
			final String destinationAddress, TaxiGangMobile taxiGangMobile) {
		Log.info("MobileNavigatorManager:createStartDestinationMarker");

		final Command startDestAndSearchCommand = new Command(){

			@Override
			public void execute() {
				Log.info("cmd:createStartDestinationMarker");
				Command destAndSearchCommand = new Command(){

					@Override
					public void execute() {
						Log.info("cmd:destAndSearch");
						
						Command searchCommand = new Command(){
							@Override
							public void execute() {
								Log.info("cmd:searchForTaxiGangs");
								TaxiGangHandler.getInstance().searchForTaxiGangs();
							}

						};
						
						navigatorView.createDestinationMarker(destinationAddress,searchCommand,true);


					}};

				navigatorView.createStartMarker(startAddress,destAndSearchCommand,false);

			}
			
		};
		executeNavigator(startDestAndSearchCommand,taxiGangMobile);
		
	}

	public static void setUpForCreation(final TaxiGangInfo info, final TaxiGangMobile taxiGangMobile) {
		Log.info("MobileNavigatorManager:setUpForCreation");

		final Command startDestAndFindCommand = new Command(){

			@Override
			public void execute() {
				Log.info("cmd:setUpForCreation");
				Command destAndFindCommand = new Command(){

					@Override
					public void execute() {
						Log.info("cmd:destAndSearch");
						
						Command searchCommand = new Command(){
							@Override
							public void execute() {
								Log.info("cmd:searchForTaxiGangs");
								taxiGangMobile.show(PanelType.MOBILENUMBER);
							}

						};
						
						navigatorView.createDestinationMarker(info.getDestination(),searchCommand,true);


					}};

				navigatorView.createStartMarker(info.getStart(),destAndFindCommand,true);

			}
			
		};
		executeNavigator(startDestAndFindCommand,taxiGangMobile);
		
	}

	public static void resetSuggestion(final TaxiGangMobile taxiGangMobile) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.resetSuggestion();				
			}
			
		};
		executeNavigator(cmd,taxiGangMobile);

		
	}

	public static void createSuggestion(final TaxiGangInfo suggestion,
			final TaxiGangMobile taxiGangMobile) {
		Command cmd = new Command(){

			@Override
			public void execute() {
				navigatorView.makeSuggestion(suggestion);
				
			}
		};
		executeNavigator(cmd,taxiGangMobile);
		
	}
}
